package com.cz.nongyetongv5.utils;

import android.util.Log;

import com.cz.nongyetongv5.bean.ChanPinJianCeRecord;
import com.cz.nongyetongv5.bean.DuChaXunChaRecord;
import com.cz.nongyetongv5.bean.JianDuJianChaRecord;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SerliazableUtil {

	public static void saveTemp(JianDuJianChaRecord jianDuJianChaRecord,
								String username) {

		ObjectOutputStream oos = null;

		FileOutputStream fos = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

			if (!jianDuJianChaDataFile.exists()) {
				jianDuJianChaDataFile.mkdirs();
			}

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");

			if (!jianDuJianChaListDataFile.exists()) {
				jianDuJianChaListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File jianDuJianChaDataTemp = new File(jianDuJianChaDataFile,
					fileName);

			// 保存为临时的
			if (jianDuJianChaRecord.getState() == 0) {

				jianDuJianChaRecord.setFileId(fileName);

				fos = new FileOutputStream(jianDuJianChaDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(jianDuJianChaRecord);

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				if (jianDuJianChaRecord.getFileId() != null
						&& jianDuJianChaRecord.getFileId().indexOf("temp") < 0) {

					File exitsFile = new File(jianDuJianChaListDataFile,
							jianDuJianChaRecord.getFileId());

					if (exitsFile.exists()) {
						fileName = jianDuJianChaRecord.getFileId();

					}

				}

				jianDuJianChaRecord.setFileId(fileName);

				jianDuJianChaDataTemp = new File(jianDuJianChaListDataFile,
						fileName);

				fos = new FileOutputStream(jianDuJianChaDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(jianDuJianChaRecord);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			fos.close();

			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}



	public static void saveTemp(DuChaXunChaRecord duChaXunChaRecord,
								String username) {

		ObjectOutputStream oos = null;

		FileOutputStream fos = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File duChaXunChaDataFile = new File(appDataFile, "duchaxuncha");

			if (!duChaXunChaDataFile.exists()) {
				duChaXunChaDataFile.mkdirs();
			}

			File duChaXunChaListDataFile = new File(duChaXunChaDataFile,
					"list");

			if (!duChaXunChaListDataFile.exists()) {
				duChaXunChaListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File duChaXunChaDataTemp = new File(duChaXunChaDataFile,
					fileName);

			// 保存为临时的
			if (duChaXunChaRecord.getState() == 0) {

				duChaXunChaRecord.setFileId(fileName);

				fos = new FileOutputStream(duChaXunChaDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(duChaXunChaRecord);

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				if (duChaXunChaRecord.getFileId() != null
						&& duChaXunChaRecord.getFileId().indexOf("temp") < 0) {

					File exitsFile = new File(duChaXunChaListDataFile,
							duChaXunChaRecord.getFileId());

					if (exitsFile.exists()) {
						fileName = duChaXunChaRecord.getFileId();

					}

				}

				duChaXunChaRecord.setFileId(fileName);

				duChaXunChaDataTemp = new File(duChaXunChaListDataFile,
						fileName);

				fos = new FileOutputStream(duChaXunChaDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(duChaXunChaRecord);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			fos.close();

			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 存储产品检测
	 *
	 * @param chanPinJianCeRecord
	 * @param username
	 */
	public static void saveTemp(ChanPinJianCeRecord chanPinJianCeRecord,
								String username) {

		ObjectOutputStream oos = null;

		FileOutputStream fos = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File chanPinJianCeDataFile = new File(appDataFile, "chanpinjiance");

			if (!chanPinJianCeDataFile.exists()) {
				chanPinJianCeDataFile.mkdirs();
			}

			File chanPinJianCeListDataFile = new File(chanPinJianCeDataFile,
					"list");

			if (!chanPinJianCeListDataFile.exists()) {
				chanPinJianCeListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File chanPinJianCeDataTemp = new File(chanPinJianCeDataFile,
					fileName);

			// 保存为临时的
			if (chanPinJianCeRecord.getState() == 0) {

				chanPinJianCeRecord.setFileId(fileName);

				fos = new FileOutputStream(chanPinJianCeDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(chanPinJianCeRecord);

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				if (chanPinJianCeRecord.getFileId() != null
						&& chanPinJianCeRecord.getFileId().indexOf("temp") < 0) {

					File exitsFile = new File(chanPinJianCeListDataFile,
							chanPinJianCeRecord.getFileId());

					if (exitsFile.exists()) {
						fileName = chanPinJianCeRecord.getFileId();

					}

				}

				chanPinJianCeRecord.setFileId(fileName);

				chanPinJianCeDataTemp = new File(chanPinJianCeListDataFile,
						fileName);

				fos = new FileOutputStream(chanPinJianCeDataTemp);

				oos = new ObjectOutputStream(fos);

				oos.writeObject(chanPinJianCeRecord);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			fos.close();

			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static List<JianDuJianChaRecord> getJianDuJianChaRecordByState(
			int state, final String username) {

		List<JianDuJianChaRecord> records = new ArrayList<JianDuJianChaRecord>();

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

			if (!jianDuJianChaDataFile.exists()) {

				jianDuJianChaDataFile.mkdirs();

			}

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");

			if (!jianDuJianChaListDataFile.exists()) {
				jianDuJianChaListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File jianDuJianChaDataTemp = new File(jianDuJianChaDataFile,
					fileName);

			// 保存为临时的
			if (state == 0) {

				if (jianDuJianChaDataTemp.exists()) {
					fis = new FileInputStream(jianDuJianChaDataTemp);

					ois = new ObjectInputStream(fis);

					JianDuJianChaRecord jianDuJianChaRecord = (JianDuJianChaRecord) ois
							.readObject();

					records.add(jianDuJianChaRecord);
				}

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				jianDuJianChaDataTemp = new File(jianDuJianChaListDataFile,
						fileName);

				File[] files = jianDuJianChaListDataFile
						.listFiles(new FilenameFilter() {

							@Override
							public boolean accept(File dir, String filename) {

								return filename.startsWith(username);
							}
						});

				for (File file : files) {

					FileInputStream fisFile = new FileInputStream(file);

					ObjectInputStream oisFile = new ObjectInputStream(fisFile);

					JianDuJianChaRecord jianDuJianChaRecord = (JianDuJianChaRecord) oisFile
							.readObject();



					records.add(jianDuJianChaRecord);

					oisFile.close();

					fisFile.close();

				}

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return records;
	}

	public static List<DuChaXunChaRecord> getDuChaXunChaRecordByState(
			int state, final String username) {

		List<DuChaXunChaRecord> records = new ArrayList<DuChaXunChaRecord>();

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File duChaXunChaDataFile = new File(appDataFile, "duchaxuncha");

			if (!duChaXunChaDataFile.exists()) {

				duChaXunChaDataFile.mkdirs();

			}

			File duChaXunChaListDataFile = new File(duChaXunChaDataFile,
					"list");

			if (!duChaXunChaListDataFile.exists()) {
				duChaXunChaListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File duChaXunChaDataTemp = new File(duChaXunChaDataFile,
					fileName);

			// 保存为临时的
			if (state == 0) {

				if (duChaXunChaDataTemp.exists()) {
					fis = new FileInputStream(duChaXunChaDataTemp);

					ois = new ObjectInputStream(fis);

					DuChaXunChaRecord duChaXunChaRecord = (DuChaXunChaRecord) ois
							.readObject();

					records.add(duChaXunChaRecord);
				}

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				duChaXunChaDataTemp = new File(duChaXunChaListDataFile,
						fileName);

				File[] files = duChaXunChaListDataFile
						.listFiles(new FilenameFilter() {

							@Override
							public boolean accept(File dir, String filename) {

								return filename.startsWith(username);
							}
						});

				for (File file : files) {

					FileInputStream fisFile = new FileInputStream(file);

					ObjectInputStream oisFile = new ObjectInputStream(fisFile);

					DuChaXunChaRecord duChaXunChaRecord = (DuChaXunChaRecord) oisFile
							.readObject();

					records.add(duChaXunChaRecord);

					oisFile.close();

					fisFile.close();

				}

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return records;
	}

	public static List<ChanPinJianCeRecord> getChanPinJianCeRecordByState(
			int state, final String username) {

		List<ChanPinJianCeRecord> records = new ArrayList<ChanPinJianCeRecord>();

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "chanpinjiance");

			if (!jianDuJianChaDataFile.exists()) {

				jianDuJianChaDataFile.mkdirs();

			}

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");

			if (!jianDuJianChaListDataFile.exists()) {
				jianDuJianChaListDataFile.mkdirs();
			}

			String fileName = username + "_" + "temp.data";

			File jianDuJianChaDataTemp = new File(jianDuJianChaDataFile,
					fileName);

			// 保存为临时的
			if (state == 0) {

				if (jianDuJianChaDataTemp.exists()) {
					fis = new FileInputStream(jianDuJianChaDataTemp);

					ois = new ObjectInputStream(fis);

					ChanPinJianCeRecord jianDuJianChaRecord = (ChanPinJianCeRecord) ois
							.readObject();

					records.add(jianDuJianChaRecord);
				}

			} else {

				fileName = username + "_" + System.currentTimeMillis()
						+ ".data";

				jianDuJianChaDataTemp = new File(jianDuJianChaListDataFile,
						fileName);

				File[] files = jianDuJianChaListDataFile
						.listFiles(new FilenameFilter() {

							@Override
							public boolean accept(File dir, String filename) {

								return filename.startsWith(username);
							}
						});

				for (File file : files) {

					FileInputStream fisFile = new FileInputStream(file);

					ObjectInputStream oisFile = new ObjectInputStream(fisFile);

					ChanPinJianCeRecord jianDuJianChaRecord = (ChanPinJianCeRecord) oisFile
							.readObject();

					records.add(jianDuJianChaRecord);

					oisFile.close();

					fisFile.close();

				}

			}
		} catch (FileNotFoundException e) {
			Log.d("debug","未见不错在");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("debug","IO错误");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			Log.d("debug","数据转换失败");
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return records;
	}

	public static void deleteAll() {

		File appDataFile = ImageUtil.getThisAppDataFile();

		File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

		if (jianDuJianChaDataFile.exists()) {

			jianDuJianChaDataFile.delete();
		}
	}

	public static void deleteAllChanPinJianCe() {

		File appDataFile = ImageUtil.getThisAppDataFile();

		File jianDuJianChaDataFile = new File(appDataFile, "chanpinjiance");

		if (jianDuJianChaDataFile.exists()) {

			jianDuJianChaDataFile.delete();
		}
	}

	public static void delete(String fileId) {

		if (CharsetUtil.isNotNull(fileId)) {

			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

			File file = new File(new File(jianDuJianChaDataFile, "list"),
					fileId);

			if (file.exists()) {

				file.delete();
			}

		}
	}

	public static void deleteChanPinJianCe(String fileId) {

		if (CharsetUtil.isNotNull(fileId)) {

			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "chanpinjiance");

			File file = new File(new File(jianDuJianChaDataFile, "list"),
					fileId);

			if (file.exists()) {

				file.delete();
			}

		}
	}


	public static void deleteDuChaXunCha(String fileId) {

		if (CharsetUtil.isNotNull(fileId)) {

			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "duchaxuncha");

			File file = new File(new File(jianDuJianChaDataFile, "list"),
					fileId);

			if (file.exists()) {

				file.delete();
			}

		}
	}

	public static void deleteTemp() {
		File appDataFile = ImageUtil.getThisAppDataFile();

		File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

		File[] files = jianDuJianChaDataFile.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {

				return pathname.isFile();
			}
		});

		for (File file : files) {
			file.delete();
		}

	}

	public static void deleteTempChanPinJianCe() {
		File appDataFile = ImageUtil.getThisAppDataFile();

		File jianDuJianChaDataFile = new File(appDataFile, "chanpinjiance");

		File[] files = jianDuJianChaDataFile.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {

				return pathname.isFile();
			}
		});

		for (File file : files) {
			file.delete();
		}

	}


	public static void deleteDuChaXunChaTemp() {
		File appDataFile = ImageUtil.getThisAppDataFile();

		File jianDuJianChaDataFile = new File(appDataFile, "duchaxuncha");

		File[] files = jianDuJianChaDataFile.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {

				return pathname.isFile();
			}
		});

		for (File file : files) {
			file.delete();
		}

	}

	public static int existsCount(final String username) {

		int count = 0;

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "jiandujiancha");

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");

			File[] files = jianDuJianChaListDataFile
					.listFiles(new FilenameFilter() {

						@Override
						public boolean accept(File dir, String filename) {

							return filename.startsWith(username);
						}
					});

			count = files.length;

		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return count;
	}

	public static int existsCountChanPinJianCe(final String username) {

		int count = 0;

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "chanpinjiance");

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");
//			File[] files = jianDuJianChaListDataFile
//					.listFiles();
			File[] files = jianDuJianChaListDataFile
					.listFiles(new FilenameFilter() {

						@Override
						public boolean accept(File dir, String filename) {
							Log.d("debugfile",dir.getAbsolutePath());
							InputStream instream;
							try {
								instream = new FileInputStream(dir+"/"+filename);

								if (instream != null) {
									InputStreamReader inputreader = new InputStreamReader(instream);
									BufferedReader buffreader = new BufferedReader(inputreader);
									String line;
									String content="";
									//分行读取
									while (( line = buffreader.readLine()) != null) {
										content += line + "\n";
									}
									instream.close();
									Log.d("debugfile",content);
								}
							} catch ( Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							return filename.startsWith(username);
						}
					});

			count = files.length;

		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return count;
	}


	public static int existsCountDuChaXunCha(final String username) {

		int count = 0;

		ObjectInputStream ois = null;

		FileInputStream fis = null;

		try {
			File appDataFile = ImageUtil.getThisAppDataFile();

			File jianDuJianChaDataFile = new File(appDataFile, "duchaxuncha");

			File jianDuJianChaListDataFile = new File(jianDuJianChaDataFile,
					"list");

			File[] files = jianDuJianChaListDataFile
					.listFiles(new FilenameFilter() {

						@Override
						public boolean accept(File dir, String filename) {

							return filename.startsWith(username);
						}
					});

			count = files.length;

		} finally {
			try {
				if (fis != null)
					fis.close();

				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return count;
	}
}
